package com.yeeiee.catalog;


import com.yeeiee.factory.ClinkFactory;
import com.yeeiee.utils.QuoteIdentifier;
import lombok.val;
import org.apache.flink.table.catalog.*;
import org.apache.flink.table.catalog.exceptions.*;
import org.apache.flink.table.catalog.stats.CatalogColumnStatistics;
import org.apache.flink.table.catalog.stats.CatalogTableStatistics;
import org.apache.flink.table.expressions.Expression;
import org.apache.flink.util.Preconditions;

import java.util.Collections;
import java.util.List;

/**
 * catalog
 */
public interface ClinkCatalog extends ClinkFactory<ClinkCatalog>, Catalog, QuoteIdentifier {

    void setConnector(String connector);

    @Override
    default CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
        Preconditions.checkNotNull(databaseName, "databaseName cannot be null");
        if (!listDatabases().contains(databaseName)) {
            throw new DatabaseNotExistException(factoryIdentifier(), databaseName);
        }

        return new CatalogDatabaseImpl(Collections.emptyMap(), null);
    }

    @Override
    default boolean databaseExists(String databaseName) throws CatalogException {
        Preconditions.checkNotNull(databaseName, "databaseName cannot be null");
        return listDatabases().contains(databaseName);
    }

    @Deprecated
    @Override
    default void alterDatabase(String name, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default List<String> listViews(String databaseName) throws DatabaseNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Override
    default boolean tableExists(ObjectPath tablePath) throws CatalogException {
        Preconditions.checkNotNull(tablePath, "tablePath cannot be null");
        val databaseName = tablePath.getDatabaseName();
        val tableName = tablePath.getObjectName();

        try {
            return listTables(databaseName).contains(tableName);
        } catch (DatabaseNotExistException e) {
            throw new CatalogException(e.getMessage());
        }
    }

    @Deprecated
    @Override
    default void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists) throws TableNotExistException, TableAlreadyExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterTable(ObjectPath tablePath, CatalogBaseTable newTable, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath) throws TableNotExistException, TableNotPartitionedException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default List<CatalogPartitionSpec> listPartitionsByFilter(ObjectPath tablePath, List<Expression> filters) throws TableNotExistException, TableNotPartitionedException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default CatalogPartition getPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default boolean partitionExists(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void createPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition partition, boolean ignoreIfExists) throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, PartitionAlreadyExistsException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void dropPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition newPartition, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default List<String> listFunctions(String dbName) throws DatabaseNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default CatalogFunction getFunction(ObjectPath functionPath) throws FunctionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default boolean functionExists(ObjectPath functionPath) throws CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void createFunction(ObjectPath functionPath, CatalogFunction function, boolean ignoreIfExists) throws FunctionAlreadyExistException, DatabaseNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void dropFunction(ObjectPath functionPath, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default CatalogTableStatistics getTableStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException {
        return CatalogTableStatistics.UNKNOWN;
    }

    @Deprecated
    @Override
    default CatalogColumnStatistics getTableColumnStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException {
        return CatalogColumnStatistics.UNKNOWN;
    }

    @Deprecated
    @Override
    default CatalogTableStatistics getPartitionStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException {
        return CatalogTableStatistics.UNKNOWN;
    }

    @Deprecated
    @Override
    default CatalogColumnStatistics getPartitionColumnStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws PartitionNotExistException, CatalogException {
        return CatalogColumnStatistics.UNKNOWN;
    }

    @Deprecated
    @Override
    default void alterTableStatistics(ObjectPath tablePath, CatalogTableStatistics tableStatistics, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterTableColumnStatistics(ObjectPath tablePath, CatalogColumnStatistics columnStatistics, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException, TablePartitionedException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterPartitionStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogTableStatistics partitionStatistics, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }

    @Deprecated
    @Override
    default void alterPartitionColumnStatistics(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogColumnStatistics columnStatistics, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
        throw new CatalogException("Not supported");
    }
}
